package com.java.module.system.service.impl;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.java.common.config.ProjectConfig;
import com.java.common.lang.exception.BaseException;
import com.java.common.utils.file.FileUploadUtils;
import com.java.module.system.domain.vo.UploadVO;
import com.java.module.system.service.ICommonService;

/**
 * 通用服务层实现
 * 
 * @author yilabao
 * @date 2021年3月22日
 */
@Service
public class CommonServiceImpl implements ICommonService {

    private static final Logger LOG = LoggerFactory.getLogger(CommonServiceImpl.class);

    @Override
    public UploadVO uploadFile(MultipartFile file) {
        String filePath = ProjectConfig.getUploadPath();
        String fileName = null;
        try {
            fileName = FileUploadUtils.upload(filePath, file);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
        if (fileName == null) {
            throw new BaseException("上传失败");
        }
        UploadVO uploadVO = new UploadVO();
        uploadVO.setUrl(fileName);
        return uploadVO;
    }

    @Override
    public UploadVO uploadBase64(String file) {
        String fileName = null;
        try {
            fileName = FileUploadUtils.upload(file, "jpg");
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        }
        if (fileName == null) {
            throw new BaseException("上传失败");
        }
        UploadVO uploadVO = new UploadVO();
        uploadVO.setUrl(fileName);
        return uploadVO;
    }

}